#ifdef MBM_IMIBASE_SUPPORT
#include "mbm_mtk_inc.h"
#include <ctype.h>

#include "mbm_typedefine.h"
#include "mbm_sys_adapter.h"
#include "mbm_memory_adapter.h"
#include "mbm_config.h"


#if defined(MBM_DEBUG_CLIB)
#define CLIB_TRACE mbm_trace
#else
#define CLIB_TRACE(...) 
#endif

Int32 mbm_atoi(ImpCStr s)
{
	return atoi(s);
}

void mbm_srand(UInt32 seed)
{
	srand(seed);
}

Int32 mbm_rand(void)
{
	return rand();
}

Int32 mbm_vsprintf(ImpStr str, ImpCStr fmt, va_list list)
{
	return _vsprintf(str, fmt, list);
}

Int32 mbm_vsnprintf(ImpStr str, Int32 n, ImpCStr fmt, va_list list)
{
	return _vsnprintf(str, n, fmt, list);
}

Int8 mbm_isdigit(Int8 c)
{
	return isdigit(c);
}

void *mbm_memcpy(void *dest, const void *src, int count)
{
	return memcpy(dest, src, count);
}

void *mbm_memset(void *dest, int c, int count)
{
	return memset(dest, c, count);
}

Int32 mbm_memcmp(const void *dest, const void *src, Int32 count)
{
	return memcmp(dest, src, count);
}

/**
 * @name mbm_vsscanf -discripe
 *
 * @param param1	-property	-discripe
 *
 * @return -discripe
 */
Int32 mbm_vsscanf(ImpCStr str, ImpCStr fmt, va_list params)
{
	ImpCStr pfmt = fmt;
	Int32 val_count = 0;
	void* val[10] = {0};
	Int32 i = 0;
	Int32 ret = 0;

	MMI_ASSERT(pfmt != NULL && str != NULL);
	
	//CLIB_TRACE("mbm_vsscanf str=%s fmt=%s", str, fmt);
	while(*pfmt != 0)
	{
		if(*pfmt == '%')
		{			
			if(pfmt[1] == '%' || pfmt[1] == '*' || pfmt[1] == '\0')
			{
				pfmt++;
				continue;
			}
			val_count++;
		}
		pfmt++;
	}

	if(val_count > 10)
	{
		CLIB_TRACE("Warning, mbm_vsscanf val too many val_count=%d", val_count);
		val_count = 10;
	}

	for(i = 0; i < val_count; i++)
	{
		val[i] = va_arg(params, void *);
		if(NULL == val[i])
		{
			break;
		}
	}

	switch(i)
	{
		case 1:
			ret = sscanf(str, fmt, val[0]);
			break;
		case 2:
			ret = sscanf(str, fmt, val[0], val[1]);
			break;
		case 3:
			ret = sscanf(str, fmt, val[0], val[1], val[2]);
			break;
		case 4:
			ret = sscanf(str, fmt, val[0], val[1], val[2], val[3]);
			break;
		case 5:
			ret = sscanf(str, fmt, val[0], val[1], val[2], val[3], val[4]);
			break;
		case 6:
			ret = sscanf(str, fmt, val[0], val[1], val[2], val[3], val[4], val[5]);
			break;
		case 7:
			ret = sscanf(str, fmt, val[0], val[1], val[2], val[3], val[4], val[5], val[6]);
			break;
		case 8:
			ret = sscanf(str, fmt, val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7]);
			break;
		case 9:
			ret = sscanf(str, fmt, val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8]);
			break;
		case 10:
			ret = sscanf(str, fmt, val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8], val[9]);
			break;
		default:
			break;
	}
	
	//CLIB_TRACE("mbm_vsscanf i=%d, ret=%d", i, ret);
	return ret;
}

Int32 mbm_sprintf(ImpStr s, ImpCStr fmt, ...)
{
	Int32 ret = 0;
	
	va_list params;
	va_start(params, fmt);
	
	mbm_vsprintf(s, fmt, params);
	
	va_end (params);

	return ret;
}

#endif/* MBM_IMIBASE_SUPPORT  */
